package org.apogee.mil.client.main.pages.admin.forms;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class AddTest extends LayoutContainer {
	private FormData formData;
	private User currentUser = null;

	public AddTest() {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());

	}


	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);
		createForm1();  
	}  

	private void createForm1() {  
		FormPanel simple = new FormPanel(); 
		simple.getHeader().setVisible(false); 
		simple.setFrame(true);  
		simple.setWidth(350);
		simple.setLabelWidth(100);

		final TextField<String> firstName = new TextField<String>();  
		firstName.setFieldLabel("Test Name");  
		firstName.setAllowBlank(false);  
		firstName.getFocusSupport().setPreviousId(simple.getButtonBar().getId());  
		firstName.setAutoWidth(true);
		simple.add(firstName, formData);
		
		final TextField<String> passwd = new TextField<String>();  
		passwd.setFieldLabel("Minimum Score");  
		passwd.setAllowBlank(false);  
		passwd.setAutoWidth(true);
		simple.add(passwd, formData);

		Button b = new Button("Create");  
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				ServerServiceFactory.getAdminService(currentUser.getSessionId()).createTest(firstName.getValue().trim() + ":" + passwd.getValue().trim(), new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if (result == false) {
							MessageBox.alert("Error", "Unable to create test", null);
							return;
						}
						
						Info.display("Success", "Test Added Successfully");
						getParent().setVisible(false);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Unable to create test\n" + caught.getMessage(), null);
						
					}
				});

			}
		});
		simple.addButton(b);  

		simple.setButtonAlign(HorizontalAlignment.CENTER);

		FormButtonBinding binding = new FormButtonBinding(simple);  
		binding.addButton(b);  

		add(simple); 

	}
}
